import  time

from auto_uitest.Task.day05 import BasePage, getDriver
from producttest.baw.loginpage import LoginPage
from producttest.caw.logger import Logger


class AddProduct(BasePage):
    dw_kehuguanli ='id = s-menu-1'
    dw_iframe='id=iframe-1'
    dw_chanping='link text=产品'
    dw_tianjiachanping='class name = btn.btn-primary'
    dw_productname='id = name'
    dw_productcode='id = code'
    dw_line='id = line'
    dw_type='id = type'
    dw_status='id = status'
    dw_fileControl='class name = fileControl'
    dw_submit='id = submit'


    # 成功
    dw_element='class name = reloadDeleter'

    #失败
    dw_nameLabel = 'id = nameLabel'
    dw_codeLabel = 'id = codeLabel'



    def addproduct(self,productname,productcode,line,type,status):
        self.click_element(self.dw_kehuguanli)
        self.switch_to_iframe(self.dw_iframe)
        self.click_element(self.dw_chanping)
        self.click_element(self.dw_tianjiachanping)

        # 添加产品，发送数据
        # 名称
        self.send_information(self.dw_productname,productname)
        # 代号
        self.send_information(self.dw_productcode,productcode)
        # 产品线选择
        self.select_by_text(self.dw_line,line)
        #类型选择
        self.select_by_text(self.dw_type,type)
        #状态
        self.select_by_text(self.dw_status,status)
        # 提交
        self.click_element(self.dw_submit)



    # 检查成功
    def check_success(self):
        element = self.locate_element(self.dw_element)
        if element is not None:
            logger = Logger()
            logger.error(f"-----保存成功------")


    # 检查失败
    def check_fail(self,errinfo):# errinfo 预期的失败原因
        # 预期和页面的失败原因进行对比预判
        if "名称" in errinfo :
            # 获取用户名的页面错误信息
            info = self.locate_element(self.dw_nameLabel).text
        elif "代号" in errinfo:
            info = self.locate_element(self.dw_codeLabel).text

        logger = Logger()
        logger.error(f"-----保存失败:info------")

        return errinfo == info


if __name__ == '__main__':
    # 获取驱动
    d=getDriver('')
    lp = LoginPage(d)
    #打开页面
    lp.open('http://localhost/ranzhi/www/')
    # 登录
    lp.login("admin", "123456")

    au=AddProduct(d)
    au.addproduct('gold','532twt','001','实体类','研发中')
    au.check_success()
    time.sleep(3)
    #退出登录
    lp.logout()
    time.sleep(3)


